//
//  LMLTabViewController.m
//  DriftDog
//
//  Created by rimi on 15/9/6.
//  Copyright (c) 2015年 廖马林. All rights reserved.
//

#import "LMLTabViewController.h"

enum {
    
    itemImageTag = 100
};


@interface LMLTabViewController ()


@property (nonatomic, assign) NSInteger selectedControllerIndex;// 选中的index
@property (nonatomic, strong) UIView    *tabbarContainerView;// 按钮容器
@property (nonatomic, strong) UIView    *childControllerContainerView;// 界面容器

//@property (nonatomic, strong) UIView    *buttonBlock;  // 按钮
@property (nonatomic, strong) UIView    *slideView;// 滑块
@property (nonatomic, strong) UIView    *tabbarButtomView;// 底块

@property (nonatomic, strong) NSArray *itemsImage;  // 图片

@property (nonatomic, strong) UITapGestureRecognizer *gesture;  // 全局手势
@property (nonatomic, assign) NSInteger currentIndex;  // 当前index


- (void)initializeUserInterface;

@end

@implementation LMLTabViewController

- (void)viewDidLoad {
    
    // 判断是否自动登录
    
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}

#pragma mark - init

- (void)initializeDataSource {

    self.itemsImage = @[@"主页按钮.png", @"帖子按钮.png", @"聊天按钮.png", @"个人按钮.png"];
}

- (instancetype)initWithViewControllers:(NSArray *)viewControllers barItemImages:(NSArray *)barItemImages {

    self = [super init];
    
    if (self) {
        
        self.selectedControllerIndex = 0;
        self.viewControllers = viewControllers;
        self.barItemImages = barItemImages;
    }
    return self;
}

- (instancetype)initWithViewControllers:(NSArray *)viewControllers {

    self = [self initWithViewControllers:viewControllers barItemImages:@[]];
    return self;
}

- (instancetype)init {

    self = [self initWithViewControllers:@[]];
    return self;
}

- (void)initializeUserInterface {

    if (self.viewControllers == 0) {
        
        return;
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self.view addSubview:self.childControllerContainerView];
    [self.view addSubview:self.tabbarContainerView];
    
    /*  添加tabbar手势  */
    
    [self.tabbarContainerView addGestureRecognizer:self.gesture];
    
    self.currentIndex = 0; // 设置目前的Index为0
    UIViewController *firstViewController = self.viewControllers.firstObject;
    [self addChildViewController:firstViewController];
    
    
    
}

#pragma mark - responds event

- (void)respondsToGesture:(UITapGestureRecognizer *)sender {

//    if (index == self.selectedControllerIndex) {
//    
//        return;
//    }
    
    CGPoint location = [sender locationInView:self.tabbarContainerView];
    
    if ([((UIImageView *)[self.tabbarContainerView viewWithTag:100]).layer.presentationLayer hitTest:location]) { // 第一个被选中
        if (_currentIndex == 0) {
        
            return;
        }
        
        _currentIndex = 0;
        
        UIViewController *currentController = self.viewControllers[self.selectedControllerIndex];
        
        [currentController.view removeFromSuperview];
        [currentController removeFromParentViewController];
        [currentController willMoveToParentViewController:nil];
        
        // 加载选中的controller
        UIViewController *selectedController = self.viewControllers[0];
        [self addChildViewController:selectedController];
        
        self.selectedControllerIndex = 0;
        
        // sliderView的动画
        [UIView animateWithDuration:0.25 animations:^{
            
            self.slideView.center = ((UIImageView *)[self.tabbarContainerView viewWithTag:100]).center;
        } completion:^(BOOL finished) {
            
            
        }];
    }else if ([((UIImageView *)[self.tabbarContainerView viewWithTag:101]).layer.presentationLayer hitTest:location]) { // 第二个
        if (_currentIndex == 1) {
            
            return;
        }
        
        _currentIndex = 1;
        
        UIViewController *currentController = self.viewControllers[self.selectedControllerIndex];
        
        [currentController.view removeFromSuperview];
        [currentController removeFromParentViewController];
        [currentController willMoveToParentViewController:nil];
        
        // 加载选中的controller
        UIViewController *selectedController = self.viewControllers[1];
        [self addChildViewController:selectedController];
        
        self.selectedControllerIndex = 1;
    
        [UIView animateWithDuration:0.25 animations:^{
            
            self.slideView.center = ((UIImageView *)[self.tabbarContainerView viewWithTag:101]).center;
        } completion:^(BOOL finished) {
            
            
        }];
    }else if ([((UIImageView *)[self.tabbarContainerView viewWithTag:102]).layer.presentationLayer hitTest:location]) { // 第三个
        if (_currentIndex == 2) {
            
            return;
        }
        
        _currentIndex = 2;
        
        UIViewController *currentController = self.viewControllers[self.selectedControllerIndex];
        
        [currentController.view removeFromSuperview];
        [currentController removeFromParentViewController];
        [currentController willMoveToParentViewController:nil];
        
        // 加载选中的controller
        UIViewController *selectedController = self.viewControllers[2];
        [self addChildViewController:selectedController];
        
        self.selectedControllerIndex = 2;
        
        [UIView animateWithDuration:0.25 animations:^{
            
            self.slideView.center = ((UIImageView *)[self.tabbarContainerView viewWithTag:102]).center;
        } completion:^(BOOL finished) {
            
        }];
    }else if ([((UIImageView *)[self.tabbarContainerView viewWithTag:103]).layer.presentationLayer hitTest:location]) { // 第四个
        if (_currentIndex == 3) {
            
            return;
        }
        
        _currentIndex = 3;
        
        UIViewController *currentController = self.viewControllers[self.selectedControllerIndex];
        
        [currentController.view removeFromSuperview];
        [currentController removeFromParentViewController];
        [currentController willMoveToParentViewController:nil];
        
        // 加载选中的controller
        UIViewController *selectedController = self.viewControllers[3];
        [self addChildViewController:selectedController];
        
        self.selectedControllerIndex = 3;
    
        [UIView animateWithDuration:0.25 animations:^{
            
            self.slideView.center = ((UIImageView *)[self.tabbarContainerView viewWithTag:103]).center;
        } completion:^(BOOL finished) {
            
        }];
    }
}

#pragma mark - override

// 重写addChildViewController方法
- (void)addChildViewController:(UIViewController *)childController {

    [self.childControllerContainerView addSubview:childController.view];
    
    [super addChildViewController:childController];
    [childController didMoveToParentViewController:self];
}

#pragma mark - getter

// 内容
- (UIView *)childControllerContainerView {

    if (!_childControllerContainerView) {
        
        _childControllerContainerView = [[UIView alloc] initWithFrame:self.view.bounds];
        
    }
    return _childControllerContainerView;
}

// tabbar的按钮
- (UIView *)tabbarContainerView {

    if (!_tabbarContainerView) {
        
        _tabbarContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - NAV_HEIGHT * DHFlexibleVerticalMutiplier(), self.view.bounds.size.width, NAV_HEIGHT * DHFlexibleVerticalMutiplier())];
        
        [_tabbarContainerView addSubview:self.tabbarButtomView]; // 背景
        
        /* 添加滑块 */
        [_tabbarContainerView addSubview:self.slideView];
        
        /* 添加模拟按钮 */
        for (int i = 0; i < 4; i ++) {
            
            UIImageView *imageViewBlock = [[UIImageView alloc] initWithFrame:CGRectMake(i * (self.view.bounds.size.width / 4), 5, self.view.bounds.size.width / 4.f, (NAV_HEIGHT - 8) * DHFlexibleVerticalMutiplier())];
            
            imageViewBlock.backgroundColor = [UIColor clearColor];
            imageViewBlock.image = [UIImage imageNamed:self.itemsImage[i]];
            imageViewBlock.tag = itemImageTag + i;  // 100 + i
            
            //imageViewBlock.image = [UIImage imageWithContentsOfFile:[NSBundle mainBundle] pathForAuxiliaryExecutable:self.itemsImage[i]] ];  // 添加图片
            
            [_tabbarContainerView addSubview:imageViewBlock];
        }
    }
    return _tabbarContainerView;
}

// 滑块
- (UIView *)slideView {

    if (!_slideView) {
        
        _slideView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width / 4.f, NAV_HEIGHT * DHFlexibleVerticalMutiplier())];
        _slideView.backgroundColor = [UIColor colorWithRed:55 / 255.f green:54 / 255.f blue:52 / 255.f alpha:1];
    }
    return _slideView;
}

// tabbar背景
- (UIView *)tabbarButtomView {

    if (!_tabbarButtomView) {
        
        _tabbarButtomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, NAV_HEIGHT * DHFlexibleVerticalMutiplier())];
        _tabbarButtomView.backgroundColor = [UIColor colorWithWhite:72 / 255.f alpha:1];
    }
    return _tabbarButtomView;
}

// 全局手势，作用是点击按钮
- (UITapGestureRecognizer *)gesture {

    if (!_gesture) {
        
        _gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToGesture:)];
    }
    return _gesture;
}


@end
